/**
 * very-easyui-1.0.0
 * 作者：剽悍一小兔
 * 官网：http://www.java520tz.com/
 */
	




	var i = 0;
	//列表渲染
	while(document.getElementById('grid' + i)){
		var config = window['grid' + i];
		var html = " <table style=\"width: 100%; height: 600px; ;\" id=\"dg" + i + "\" title=\""+config.title+"\" class=\"easyui-datagrid\" fitColumns=\"true\" "+
					" pagination=\""+config.paging+"\" singleSelect=\""+config.singleSelect+"\" rownumbers=\"true\" url=\""+ctx+"/"+config.url+"\" fit=\"true\" toolbar=\"#tb" + i + "\">"+
					" <thead>"+
					" <tr>";
		if(config.hasSelect)
			html += " <th field=\"cb\" checkbox=\"true\" align=\"center\"></th>";
		//字段渲染		
		var fields = config.fields;		
		for(var j=0; j < fields.length; j++){
			var arr = fields[j].split("|");
			var formatterHTML = '';
			if(arr[3]){
				var formatter = arr[3];
				if(formatter == 'download'){
					formatterHTML = " formatter='downloadFormatter' ";
				}
				if(formatter == 'view'){
					formatterHTML = " formatter='viewFormatter' ";
				}
			}
			html += " <th " + formatterHTML + (arr[1].indexOf('Time')!=-1?"formatter='timeFormat'":"") +" field=\""+arr[1]+"\" width=\""+arr[0]+"\" align=\"center\">"+arr[2] + "</th>";
		}			
					
		html +=		" </tr>"+
					" </thead>"+
					" </table>";
		//alert(html);			
		
		$('#grid' + i).html(html);
		
		
		//工具栏渲染
		html = " <div id=\"tb0\" >"+
				" <div>";
				
		var buttons = config.buttons;
		
		for(var k = 0;k < buttons.length;k++){
			html	+=" <a style=\"margin:2px\" href=\"javascript:"+buttons[k].actionName+"()\" class=\"easyui-linkbutton\"  >"+buttons[k].name+"</a>";
		}
		
		
				
		html	+=" </div>"+
				" <div>";
		
				
		//搜索栏渲染		
		var searchFields = config.search.fields;
		
		for(var k = 0;k < searchFields.length;k++){
			html+=" &nbsp;"+searchFields[k].split('|')[1]+"：&nbsp;<input type=\"text\" name=\""+searchFields[k].split('|')[0]+"\" id=\""+searchFields[k].split('|')[0]+"\"  onkeydown=\"if(event.keyCode==13) searchList"+i+"()\"/>";
		}		
				
		if(searchFields.length > 0) {
			html+=	" <a href=\"javascript:searchList"+i+"()\" class=\"easyui-linkbutton\" iconCls=\"icon-search\" plain=\"true\">搜索</a>"+
			" </div>"+
			" </div>";
		}
			
		
		//alert(html);
		
		
		$("body").append(html);
		
		html = '';
		
		//拼接搜索方法
		
		eval(" function searchList"+i+"(){   "+
				" $('#dg"+i+"').datagrid('load',{"+
				" "+searchFields[0].split('|')[0]+":$(\"#"+searchFields[0].split('|')[0]+"\").val(),"+  //默认第一个就是名称搜索
				" });"+
				' }');
		
		
		i++;
	}

	
	//打开一个拥有表单的窗口
	function openWin(width,height,option,selectRow){
		
		var html = " <div id=\"dlg\" class=\"mydialog\" style=\"width: "+width+"px;height: "+height+"px;"+
					" closed=\"true\" buttons=\"#dlg-buttons\"><div class='header'>"+option.title+"</div>"+
					" <div style='padding:10px 20px;'><form id=\"fm\" method=\"post\">"+
					" <table cellspacing=\"5px;\">";
					
		for(var i = 0; i < option.fields.length;i++){
			var field = option.fields[i];
			if(field.type == 'text'){
				html += " <tr>"+
							" <td>"+field.name+"：</td>"+
							" <td><input "+(field.id=='id'?"readonly":"")+" style='width:100%' type=\"text\" id=\""+field.id+"\" name=\""+field.id+"\" class=\"easyui-validatebox\" required=\"true\"/></td>"+
							" </tr>";
			}
			
			if(field.type == 'textarea'){
				html += " <tr>"+
							" <td>"+field.name+"：</td>"+
							" <td><textarea style='width:100%;height:100px;' type=\"text\" id=\""+field.id+"\" name=\""+field.id+"\"  required=\"true\"></textarea></td>"+
							" </tr>";
			}
			
			if(field.type == 'file'){
				html += " <tr>"+
							" <td>"+field.name+"：</td>"+
							" <td><input  type=\"text\" id=\""+field.id+"\" name=\""+field.id+"\"  required=\"true\">"+
						   "<a href='javascript:openUploadWin(\""+field.id+"\")'>上传</a></input></td>"+
							" </tr>";
			}
			
		}			
		
		//默认的提交按钮
		if(option.url) html += " </table> </form>"+" <a href=\"javascript:saveAction()\" class=\"linkbutton\" iconCls=\"icon-save\" >提交</a>"+"</div></div>";
		
		//$("#dlg").dialog("open").dialog("setTitle",option.title);
		
		
		$("body").append(html);
		$("#dlg").get(0).style.marginLeft = - (width / 2) + 'px';
		
		$("#fm").form("load",selectRow);
		
		$("#dlg").siblings().click(function(){
			$("#dlg").remove();
		});
		
		//注入保存方法
		if(option.url) html = "window.saveAction = function (){ $(\"#fm\").form(\"submit\",{"+
				" url:'" + ctx + option.url + "',"+
				" onSubmit:function(){"+
				" return $(this).form(\"validate\");"+
				" },"+
				" success:function(result){"+
				" console.log(result);"+
				" result = eval(\"(\"+result+\")\");"+
				" if(result.code != \"0\"){"+
				" $.messager.alert('系统提示',\"<font color=red>\"+result.errMsg+\"</font>\");"+
				" return;"+
				" }else{"+
				" $.messager.alert('系统提示','操作成功');"+
				" "+
				//" $(\"#dg\").datagrid(\"reload\");"+
				" }"+
				" }"+
				" });}"
		eval(html);
		
	}



	function post(url,params,callback){
		$.post(ctx+url,params,function(result){
			result = eval("("+result+")");
			console.log(result);
			if(result.code != "0"){
				$.messager.alert('系统提示',"<font color=red>"+result.errMsg+"</font>");
				return;
			}else{
				
				if(callback){
					callback();
				}else{
					$.messager.alert('系统提示','操作成功');
				}
			}
		});
	}


	function openUploadWin(id){
		$('#UploadWin').dialog("open").dialog("setTitle","上传");
		this.uploadId = id;
	}



	function addAppendix(name,url){
		$('#UploadWin').dialog("close");
		$('#'+uploadId).val(url);
	}
	
	function M(msg){
		$.messager.alert('系统提示',msg);
	}
	
	function timeFormat(val){
		return val;
		/*val = new Date(val.time);
		console.log(val);
		return val.toLocaleString().replace('/','年').replace('/','月').replace('/','日');*/
	}
	
	function downloadFormatter(val){
		if(!val) return "没有附件";
		val = ctx + "/" + val;
		return "<a href='"+val+"'>下载</a>";
	}
	
	function viewFormatter(val){
		if(!val) return "————";
		
		return "<a href='JavaScript:viewSelected()'>"+val+"</a>";
	}
	
	//默认查看grid0的，目前不做更多扩展
	function viewSelected(){
		var selectedRows=$("#dg0").datagrid('getSelections');
		var row=selectedRows[0];
		
		var option = {
				title : '查看',
				fields : [
				          {id:'code1',name:'发件人',type:'text'},
				          {id:'touserid',name:'收件人',type:'text'},
				          {id:'content',name:'邮件内容',type:'textarea'},
				         ],
				url : ''         
			}
		
		openWin(500,"auto",option,row);
	}
	
